#!/usr/bin/env python

# This file is part of the Possumcam project 
# at http://code.google.com/p/possumcam/
#
# Released under the GPLv3 at http://www.gnu.org/copyleft/gpl.html
# Designed for Kenneth Lavrsen's motion project at
# http://www.lavrsen.dk/twiki/bin/view/Motion
#
# Copyright 2008 by Jackson Yee (jackson@gotpossum.com)

import pygtk
pygtk.require('2.0');

import gtk
import gtk.gdk as gdk
import gtk.keysyms as keysyms
import sys
import os
import os.path
import gobject
import random
import gc
import pango
import re
import lumenn
import stat
import time
import threading

AppName = 'PossumCam Viewer'
AppVersion = '2008-10-08'
conf = lumenn.Config(AppName)
	
# *********************************************************************
class MainWnd(gtk.Window):
	
	# -------------------------------------------------------------------
	def __init__(self):
		gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
		
		x, y, w, h = conf.get('wndpos', (0, 0, 1024, 768) )
		
		if w < 400:
			w = 400
		
		if h < 300:
			h = 300
		
		self.table = gtk.Table(2, 2, True)
		
		self.images = []
		
		for i in range(0, 9):
			img = lumenn.Image()
			self.images.append( img )
			self.table.attach(img, i % 3, i % 3 + 1, i / 3, i / 3 + 1)
		
		self.add(self.table)
		
		self.connect('destroy', self.OnDestroy)
		self.connect('delete_event', self.OnDelete)
		
		self.move(x, y)
		self.resize(w, h)
		
		self.set_title(AppName + ': ' + AppVersion)
		
		self.show_all()	
		
		gobject.timeout_add( 1000, self.RefreshImages )
			
	# -------------------------------------------------------------------
	def OnKeyPress(self, wnd, evt, data = None):
		pass
		#if evt.keyval == keysyms.space or evt.keyval == keysyms.Right:
		#	self.Next()
		
	# -------------------------------------------------------------------
	def OnDelete(self, wnd, data = None):
		x, y = self.get_position()
		w, h = self.get_size()
		conf['wndpos'] = (x, y, w, h)
		
	# -------------------------------------------------------------------
	def OnDestroy(self, wnd, data = None):
		gtk.main_quit()
		
	# -------------------------------------------------------------------
	def Run(self):
		gtk.main()
		
	# -------------------------------------------------------------------
	def RefreshImages(self):
		for i in range(0, 8):
			self.images[i].Load('/mnt/storage/motion/snapshot-%i.jpg' % (i + 1) )
			
		gc.collect()
		gobject.timeout_add( 1000, self.RefreshImages )
		
# *********************************************************************
if __name__ == '__main__':
	gtk.gdk.threads_init()
	conf.Load()
	wnd = MainWnd()
	wnd.Run()
	conf.Save()
	

